import { Component, OnInit } from '@angular/core';
import { Store } from '@ngxs/store';
import { Navigate } from '@ngxs/router-plugin';
import { SellerService } from '../seller.service';
import { Stores } from '../seller.model';
import { Seller } from '../../../store/seller';

@Component({
  selector: 'app-store',
  templateUrl: './store.component.html',
  styleUrls: ['./store.component.scss'],
})
export class StoreComponent implements OnInit {
  listOfData: Stores[];

  constructor(
    private store: Store,
    private service: SellerService,
  ) {
  }

  ngOnInit(): void {
    this.load();
  }

  load() {
    this.service.storeList().subscribe(r => {
      this.listOfData = r;
    });
  }

  newStore() {
    this.store.dispatch(new Navigate(['/seller/store/new']));
  }

  activate(data: Stores) {
    this.service.active(data.id).subscribe(() => {
      this.store.dispatch(new Seller.Active(data.id));
      this.load();
    });
  }

  edit(id: number) {
    this.store.dispatch(new Navigate(['/seller/store/edit', id]));
  }

  delete(id) {
    this.service.deleteStore(id).subscribe(() => {
      this.load();
    });
  }


}
